/*
 * title: vue页面共用参数、方法
 * date: 2019-05-15
 * author: wjh
 */

export default {
  computed: {},
  data() {
    return {
      list: [], // 列表
      form: {
        pageNum: 1, // 页码
        pageSize: 10 // 每页条数
      },
      id: '', // id
      selectionList: [], // 当前选中的数据
      addVisible: false, // 新增是否显示
      editVisible: false, // 修改是否显示
      detailVisible: false, // 详情是否显示
      total: 0, // 总条数
      loading: false, // loading
      screenVisible: false, // 筛选 是否显示
      screenNum: 0, // 筛选 项数
      screenForm: {} // 筛选表单,
    }
  },
  props: {
    apiUrl: {
      require: true,
      default: 'api/article/home/99'
    }
  },
  methods: {
    // 获取列表
    async $getList(path, params, type) {
      type = type || params
      // 如果params为空,那么form表单就作为查询条件
      if (!params || typeof params === 'string') {
        params = this.form
      }
      this.loading = true
      const result = await this.$getData(path, params)
      this.loading = false
      if (!result) {
        return
      }
      const { list, totalRecord } = result
      // 如果type等于direct表示data就是列表数据,可以直接赋值
      if (type === 'direct') {
        this.list = list || []
        return false
      }
      // 如果type等于push,结果则追加到list里面
      if (type === 'push') {
        list && (this.list = this.list.concat(list))
      } else {
        this.list = list || []
      }
      this.total = totalRecord
    },
    // 新增
    $add() {
      this.id = ''
      this.addVisible = true
    },
    // 编辑
    $edit(id) {
      this.id = id
      this.editVisible = true
    },
    // 详情
    $detail(id) {
      this.id = id
      this.detailVisible = true
    },
    // 筛选
    $screen() {
      this.id = ''
      this.screenVisible = true
      this.screenForm.form = this.form
    },
    // 筛选 回调
    $screenPick(form) {
      let numArray = ['pageNum', 'pageSize']
      for (let i in form) {
        if (form[i] === '' || form[i] === undefined || form[i] === null) {
          delete form[i]
        }
      }
      form.pageNum = 1
      let numList = Object.keys(form)
      let intersections = numList.filter(e => numArray.indexOf(e) === -1)
      this.screenNum = intersections.length
      this.screenVisible = false
      this.form = form
      this.getList()
    },
    // 批量删除
    async $del(path, params) {
      let msg = (params && params.msg ? params.msg : '删除此数据, 是否继续?')
      const status = await this.$CONFIRM(msg)
      if (!status) {
        return false
      }
      this.loading = true
      const code = await this.$postCode(path, { id: params.id })
      this.loading = false
      if (code === true) {
        this.getList()
      }
      return code
    },
    // 重置
    $refresh(status) {
      this.screenNum = 0
      this.form = {
        pageNum: 1, // 页码
        pageSize: 10 // 每页条数
      }
      this.getList()
    },
    // 获取id
    $getId(val = 'id') {
      return this.selectionList.map(item => item[val])
    },
    // id处理
    $idProcess(msg) {
      const id = this.$getId()
      if (!id.length) {
        this.$message.error(`请选择要${msg}的数据`)
        return false
      }
      if (id.length > 1) {
        this.$message.error(`只能选择一条要${msg}的数据`)
        return false
      }
      return id.toString()
    },
    // 返回
    $close(fresh, freshName) {
      if (fresh === true) {
        this.$emit(`global:${freshName}`)
      }
      this.$emit('update:visible', false)
    }
  }
}
